<?php
class Shop_Model_Image extends Promotor_Model_Scaffold {

	const UPLOAD_PATH = 'upload/shop/product/';
	
	protected $_dbTableClass = 'Shop_Model_DbTable_Image';
	
	protected $_cachedMethods = array(
		'findAllByProductId'
	);
	
	public function selectList() {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('si' => 'shop_image'))
			   ->joinLeft(
			   		array('shc' => 'shop_has_category'),
			   			'si.product_id = shc.product_id', array('category_id'))
			   	->joinLeft(
			   		array('sp' => 'shop_product'),
			   			'sp.id = si.product_id', array('product' => 'name'))
			   	->joinLeft(
			   		array('sc' => 'shop_category'),
			   			'sc.id = shc.category_id', array('category' => 'name'));

		return $select;
	}
	
	public function selectListForProductPK($primaryKey) {
		$db = $this->getDbTable()
				   ->getAdapter();

		if (is_array($primaryKey)) {
			$primaryKey = current($primaryKey);
		}
				   
		$select = new Zend_Db_Select($db);

		$select->from(array('si' => 'shop_image'))
			   ->joinInner(
			   		array('sp' => 'shop_product'),
			   			'sp.id = si.product_id', 
			   			array(new Zend_Db_Expr('(si.id = sp.image_id) AS `main_image`')))

			   ->where('sp.id = ?', $primaryKey, Zend_Db::INT_TYPE);

		return $select;
	}
	
	/**
	 * @param integer $productId
	 * @return array
	 */
	public function findAllByProductId($productId) {
		$table = $this->getDbTable();
		
		$select = $table->select()
			->where('product_id = ?', $productId);
		
		$result = null;
		
		try {
			$result = $table->fetchAll($select)->toArray();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}

		return $result;
	}
 	
	/**
	 * @param array $data
	 * @return mixed
	 */
	public function save(array $data) {
		$table = $this->getDbTable();
		$row = $table->createRow($data);

		if (isset($row->publicated)) {
			$row->publicated = 1;
		}

		$result = null;
		
		try {
			$result = $row->save();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}

		return $result;
	}
	
	/**
	 * @param integer $productId
	 * @param array $images
	 * @return void
	 */
	public function attacheImageToProduct($productId, array $images) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		
		// filtrowanie przed zduplikowanymi kluczami!
		$images = array_keys(array_flip($images));

		$db->beginTransaction();
		try {
			foreach ($images as $image) {
				if (!is_array($image)) {
					$image = isset($image['name'])
						? $image['name']
						: current($image);
				}

				$table->insert(array(
					'product_id' => $productId,
					'image' => $image,
					'publicated' => 1
				));
			}
			$db->commit();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$db->rollBack();
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
}